'use strict';

// 发红包算法
// 随机

function hongbao(total, num) {
  // 宕机
  const arr = [];
  // 余额
  let restAmount = total;
  // 未领取人数
  let restNum = num;

  for (let i = 0; i < num - 1; i++) {
    let amount = parseFloat(
      Math.random() * ((restAmount / restNum) * 2)
    ).toFixed(2);
    restAmount -= amount;
    restNum--;
    arr.push(amount);
  }

  arr.push(restAmount.toFixed(2));
  return arr;
}

console.log(hongbao(20, 20));

// const random = (Math.random() * 0.8).toFixed(2);

// function bag(total, num) {
//   const arr = [];
//   let m = total;
//   for (let i = 0; i < num - 1; i++) {
//     let single = (m * Math.random() * random).toFixed(2);
//     m -= single;
//     arr.push(single);
//   }
//   arr.push(m.toFixed(2));
//   return arr;
// }

// console.log(bag(10, 10));
